import { defineStore } from "pinia";
import { postLogin } from "@/api/user";

const init = {
  userInfo: {
    account: "",
    phone: "",
    sex: "",
  },
};

export const useUserStore = defineStore("user", {
  state: () => ({ ...init }),
  actions: {
    async login(form) {
      const {
        error,
        data: { userInfo },
      } = await postLogin(form);
      if (!error) {
        this.userInfo = { ...this.userInfo, ...userInfo };
      }
      return !error;
    },
    logout() {
      this.$reset();
    },
  },
  persist: {
    enabled: true,
    detached: true, // 设置订阅与组件分离
  },
});
